@using DNA.Mvc.Controllers;
@using System.Reflection;

@model IEnumerable<PermissionSet>
<style type="text/css">
    #permsAccordion .ui-accordion-content { padding: 2px !important; }
    #permsAccordion .ui-accordion-content a { color: #000000 !important; }
    #permsAccordion .ui-accordion-content .d-listbox { width: auto; border:none;}
</style>
<div id="permsContainer">
    @*@using (Ajax.BeginAjaxForm("Apply", "Security", new jQueryAjaxOptions() { AutoBlocking = true, UpdateTargetID = "permsContainer" }, null))*@
    @using (Html.BeginForm("Apply","Security"))
    { 
        @Html.AntiForgeryToken() 
        
        Ajax.Dna().Accordion("permsAccordion")
       .Sections(views =>
       {
           foreach (var permset in Model)
           {
               var perms = permset.Permissions;
               //permset.GetPermissions();
               var permsetTitle = permset.Name;

               if (!string.IsNullOrEmpty(permset.ResbaseName) && (!string.IsNullOrEmpty(permset.TitleResName)))
               {
                   permsetTitle=Html.Global(permset.ResbaseName,permset.TitleResName);
                   if (string.IsNullOrEmpty(permsetTitle)) permsetTitle = permset.Name;
               }
               
               views.Add(permsetTitle)
                   .Template(() =>
                   {
                       Ajax.DnaFor().ListBox(perms)
                           .Height(400)
                           .ItemTemplate(@<text>
        @{
            var permValue =WebSiteContext.Current.DataContext.Permissions.IsAuthorize(item, ViewData["Role"].ToString());
            //var attr = item.SecurityAction;

            var type = Type.GetType(item.Controller + "," + item.Assembly);
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
            MethodInfo action = (from MethodInfo method in methods
                                 where (method.GetCustomAttributes(typeof(SecurityActionAttribute), true).Length > 0) && method.Name.Equals(item.Action)
                                 select method).FirstOrDefault();
            var attr = (SecurityActionAttribute)Attribute.GetCustomAttribute(action, typeof(SecurityActionAttribute));
            
            var permTitle = attr.Title;
            var permDesc = attr.Description;

            if (!string.IsNullOrEmpty(attr.ResBaseName))
            {
                if (!string.IsNullOrEmpty(attr.TitleResName))
                {
                    permTitle = Html.Global(attr.ResBaseName, attr.TitleResName);
                }

                if (!string.IsNullOrEmpty(attr.DescResName))
                {
                    permDesc = Html.Global(attr.ResBaseName, attr.DescResName);
                }
            }
            
        }
        @Ajax.Dna().Checkbox("perm_" + item.ID.ToString(), permTitle, permValue, Url.Content("~/content/images/icon_accept_16.png"), Url.Content("~/content/images/icon_block_16.png")).HtmlAttributes(new { style = "padding-top:5px;" }).GetHtml()
        <div style="clear: left; padding: 5px 0px 5px 20px; color: #6c6c6c; white-space: normal;
            border-bottom: 1px solid #cccccc;">
            @permDesc
        </div>
        </text>).Render();
                   });
           }
       })
           .Render();
                
        @Html.Hidden("roleName", ViewData["Role"].ToString()) 
        if (ViewData["Role"].ToString() != "administrators")
        { 
        <div id="buttonsPane" style="padding: 5px; padding-top: 20px;">
            @Ajax.Dna().Button("SecurityApplyButton").Submit().Text(Html.Global("RES_APPLY")).ImageIcons(Url.Content("~/content/images/icon_accept_16.png")).GetHtml()
        </div>
        }
    }
</div>
@Html.StartupScripts()
